package com.chuangke.common.sqlparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum CbExpOperatorEnum {

    AND("AND", "AND"), OR("OR", "OR");

    private String code;
    private String desc;

    CbExpOperatorEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static Map<String, Map<String, Object>> toMap() {
        CbExpOperatorEnum[] ary = CbExpOperatorEnum.values();
        Map<String, Map<String, Object>> enumMap = new HashMap<>();
        for (CbExpOperatorEnum cbExpOperatorEnum : ary) {
            Map<String, Object> map = new HashMap<>();
            String key = cbExpOperatorEnum.getCode();
            map.put("code", cbExpOperatorEnum.getCode());
            map.put("desc", cbExpOperatorEnum.getDesc());
            enumMap.put(key, map);
        }
        return enumMap;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List toList() {
        CbExpOperatorEnum[] ary = CbExpOperatorEnum.values();
        List list = new ArrayList();
        for (CbExpOperatorEnum cbExpOperatorEnum : ary) {
            Map<String, String> map = new HashMap<>();
            map.put("code", cbExpOperatorEnum.getCode());
            map.put("desc", cbExpOperatorEnum.getDesc());
            list.add(map);
        }
        return list;
    }

    public static CbExpOperatorEnum getEnum(String code) {
        CbExpOperatorEnum[] array = CbExpOperatorEnum.values();
        for (CbExpOperatorEnum cbExpOperatorEnum : array) {
            if (cbExpOperatorEnum.getCode().equalsIgnoreCase(code)) {
                return cbExpOperatorEnum;
            }
        }
        return null;
    }

    /**
     * 取枚举的json字符串
     *
     * @return
     */
    public static String getJsonStr() {
        CbExpOperatorEnum[] enums = CbExpOperatorEnum.values();
        StringBuilder jsonStr = new StringBuilder("[");
        for (CbExpOperatorEnum senum : enums) {
            if (!"[".contentEquals(jsonStr)) {
                jsonStr.append(",");
            }
            jsonStr.append("{code:'").append(senum.getCode()).append("',desc:'").append(senum.getDesc()).append("'}");
        }
        jsonStr.append("]");
        return jsonStr.toString();
    }
}
